#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Main GUI

Created by Supun
Version 1.0

Thanks to: Jan Bodnar (www.zetcode.com)
'''

import wx
import subprocess
import time


class MailFrame(wx.Frame):
    
	def __init__(self, *args, **kwargs):
		super(MailFrame, self).__init__(*args, **kwargs) 
            
		self.InitUI()
        
	def InitUI(self):    
		self.CreateStatusBar()
		menubar = wx.MenuBar()
		fileMenu = wx.Menu()
		viewMenu = wx.Menu()
		panel = wx.Panel(self)
		self.cbtn = wx.Button(panel, label='Close', pos=(20,30))
		self.pbtn = wx.Button(panel, label='Pop', pos=(110,30))
		self.pcbtn = wx.Button(panel, label='Close Pop', pos=(210,30))
	
		self.qmi = fileMenu.Append(wx.ID_EXIT, '&Quit')        
		self.Bind(wx.EVT_MENU, self.OnQuit, self.qmi)
		self.Bind(wx.EVT_BUTTON, self.OnClose, self.cbtn)
		self.Bind(wx.EVT_BUTTON, self.OnPop, self.pbtn)
		self.Bind(wx.EVT_BUTTON, self.OnClosep, self.pcbtn)

		menubar.Append(fileMenu, '&File')
		menubar.Append(viewMenu, '&View')
		self.SetMenuBar(menubar)

		self.SetSize((350, 250))
		self.SetTitle('SELinux Manipulator Framework')
		self.Centre()
		self.Show(True)
	
		self.SetStatusText("Initializing .")
#		time.sleep(2)	
		self.SetStatusText("Initializing ..")
#		time.sleep(2)	
		self.SetStatusText("Initializing ...")
		seStatus = self.SeStatus()
		self.SetStatusText("SELinux is "+seStatus)
		if "Enforcing"  in seStatus:
			pass
        		dlg = wx.MessageDialog(self, 'SELinux is not Enabled in your System.\nEnable now ?', 'SEStatus', wx.YES_NO|wx.ICON_INFORMATION)
        		result = dlg.ShowModal()
        		dlg.Destroy()
			if result == wx.ID_YES:
				self.SetStatusText("SELinux is Enabling")
				SeEn()
			else:
				self.Close()
        
	def OnQuit(self, e):
		self.Close()
	def OnClose(self, e):
		self.Close()
	def OnClosep(self, e):
		self.frame.Close()
	def OnPop(self, event):
		self.frame = SecondFrame()
           	self.frame.Show()

	def SeStatus(self):
		# Get SELinux Status
		p = subprocess.Popen("getenforce", stdout=subprocess.PIPE, shell=True)
        	(output, err) = p.communicate()
		return output
	def SeEn(self):
		# Enable SELinux
		


class SecondFrame(wx.Frame):
	#----------------------------------------------------------------------
	def __init__(self):
		wx.Frame.__init__(self, None, title="Second Frame")
		panel = wx.Panel(self)
		self.SetSize((250, 150))
		self.Centre()
		
		font = wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD)
		
		heading = wx.StaticText(self, -1, 'Scanning and fix SELinux Status', (0, 30))
		heading.SetFont(font)
		p = subprocess.Popen("getenforce", stdout=subprocess.PIPE, shell=True)
		(output, err) = p.communicate()
		if output is not "Enforcing":
			wx.StaticText(self, -1, 'SELinux Status is '+output, (40, 50))
			MyApp()
		print "SELinux status is ", output

def main():  
	ex = wx.App()
	MailFrame(None)
	ex.MainLoop()    

if __name__ == '__main__':
	main()

